<?php
/**
 * +----------------------------------------------------------------------
 * | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
 * +----------------------------------------------------------------------
 * | File name: SettingUploadImage.php
 * +----------------------------------------------------------------------
 * | Author: lijiafei
 * +----------------------------------------------------------------------
 * | Description:
 * +----------------------------------------------------------------------
 * | Others: 其他内容的说明
 * +----------------------------------------------------------------------
 * | History:
 * +----------------------------------------------------------------------
 * | 1.Date: 2019/1/3 Author: lijiafei Modification: 创建
 * +----------------------------------------------------------------------
 */

namespace App\Http\Controllers\Backend\Common;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadFileController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:backend');
    }

    const IMAGE_UPLOAD_PATH = "backend/home_image/";
    const FILE_UPLOAD_PATH = "backend/home_file/";
    public function __invoke(Request $request)
    {
        $file_type = $request->input('file_type','image');
        $upload_path = $request->input('path','image');
        if($request->hasFile('file')){
            $file = $request->file('file');
            $fileName = $file->getClientOriginalName();
            $fileNameArray = explode('.',$fileName);
            $extension = $fileNameArray[1];
            if(!in_array($extension,['jpg','png','jpeg','bmp','gif','svg'])){
                $file_type = 'file';
            }
            //上传图片
            if($file_type === 'image'){

                $is_validate = $this->validateImage($file);
                if(!$is_validate){
                    return failed('请上传正确格式文件');
                }

                $upload_path = self::IMAGE_UPLOAD_PATH . $upload_path;
                $fileInfo = $this->upload($file,$upload_path,$fileNameArray);

            }else if($file_type === 'file'){

                //上传文档
                $upload_path = self::FILE_UPLOAD_PATH . $upload_path;
                $fileInfo = $this->upload($file,$upload_path,$fileNameArray);

            }
            $fileInfo['url'] = getOssUrl($fileInfo['path']);
            return success($fileInfo);
        }else{
            return failed('请上传文件');
        }
    }

    protected function validateImage($file){
        //设置上传图片的大小小于2兆
        $filesize = $file->getSize();
        if($filesize > config('common.backend_max_file_size')){
            return false;
        }
        return true;
    }

    protected function upload($file,$upload_path,$fileNameArray){
        $fileName = $fileNameArray[0];
        $extension = $fileNameArray[1];
        $save_file_name = md5(time() . 'file' . 'kskd') . '.' . $extension;
        $path = Storage::disk('oss')->putFileAs($upload_path,$file,$save_file_name);
        return ['path' => $path,'fileName' => $fileName];
    }
}